import { createWebHashHistory, createRouter } from 'vue-router'

import Login from '../views/Login.vue'
import Home from '../views/Home.vue';
import Admin from '../views/Admin.vue';
import Matches from '../components/Matches.vue';
import Viewproject from '../components/Viewproject.vue';
import Projectrevise from '../components/Projectrevise.vue';
import Printpreview from '../components/print/Printpreview.vue';
import Projectdifficulty from '../components/print/Projectdifficulty.vue';
import Allitems from '../components/print/Allitems.vue';
import Information from '../components/Information.vue';
import NewPersonnel from '../components/NewPersonnel.vue';
import Teamdifficulty from '../components/print/Teamdifficulty.vue';
import Teambasic from '../components/print/Teambasic.vue';
import Personnel from '../components/Personnel.vue';
import Solopractice from '../components/Solopractice.vue';
import DeclarationDifficulty from '../components/DeclarationDifficulty.vue';
import ApplicationDetail from '../components/ApplicationDetail.vue';
import Entergroup from '../components/Entergroup.vue';
import ViewApplicationDetail from '../components/ViewApplicationDetail.vue';
import Provisionsdeclaration from '../components/Provisionsdeclaration.vue';
import Reportordeclare from '../components/Reportordeclare.vue';
import Induct from '../views/Induct.vue';
import Submit from '../components/Submit.vue';
import Allpersonnel from '../components/Allpersonnel.vue';
import Adminchild from '../components/Adminchild.vue';
import Allteamel from '../components/Allteamel.vue';
import Homechild from '../components/Homechild.vue';
import Fullinformation from '../components/print/Fullinformation.vue';
import Registration from '../components/Registration.vue';
import Hometeamapp from '../components/print/Hometeamapp.vue';
import Accesscontrol from '../components/Accesscontrol.vue';
import Documents from '../components/Documents.vue';
// import Ceshi from '../views/Ceshi.vue';

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    // {
    //   path:'/',
    //   name:'Ceshi',
    //   component:Ceshi
    // }
    // ,
    {
      path:'/',
      name:'Induct',
      component:Induct
    }
    ,
    {
      path:'/admin',
      name:'Admin',
      component:Admin,
      children:[{
        path: 'matches',
        name: 'Matches',
        component: Matches
      },{
        path:'adminchild',
        name:'Adminchild',
        component: Adminchild
      },{
        path:'viewproject',
        name:'Viewproject',
        component: Viewproject
      },{
        path: 'projectrevise',
        name: 'Projectrevise',
        component: Projectrevise
      },{
        path:'projectdifficulty',
        name:'Projectdifficulty',
        component: Projectdifficulty
      },{
        path:'allitems',
        name:'Allitems',
        component: Allitems
      },{
        path:'teambasic',
        name:'Teambasic',
        component: Teambasic
      },{
        path:'teamdifficulty',
        name:'Teamdifficulty',
        component: Teamdifficulty
      },{
        path:'applicationdetail',
        name:'ApplicationDetail',
        component: ApplicationDetail
      },{
        path:'viewapplicationdetail',
        name:'ViewApplicationDetail',
        component: ViewApplicationDetail
      },{
        path:'entergroup',
        name:'Entergroup',
        component: Entergroup
      },{
        path:'reportordeclare/:id',
        name:'Reportordeclare',
        component: Reportordeclare
      },{
        path:'submit',
        name:'Submit',
        component: Submit
      },{
        path:'allpetsonnel',
        name:'Allpetsonnel',
        component: Allpersonnel
      },{
        path:'allteamel',
        name:'Allteamel',
        component: Allteamel
      },{
        path:'printpreview',
        name:'Printpreview',
        component: Printpreview
      },{
        path:'registration',
        name:'Registration',
        component: Registration
      },{
        path:'accesscontrol',
        name:'Accesscontrol',
        component: Accesscontrol
      },{
        path:'documents',
        name:'Documents',
        component: Documents
      }]
    },{
    path: '/login',
    name: 'Login',
    component: Login,
    },{
    path: '/home',
    name: 'Home',
    component: Home,
    children:[{
      path:'homechild',
      name:'Homechild',
      component: Homechild
    },{
      path: 'information',
      name: 'Information',
      component: Information
    },{
      path: 'newpersonnel',
      name: 'NewPersonnel',
      component: NewPersonnel
    },{
      path: 'personnel',
      name: 'Personnel',
      component: Personnel
    },{
      path:'solopractice',
      name:'Solopractice',
      component: Solopractice
    },{
      path:'declarationdifficulty',
      name:'DeclarationDifficulty',
      component: DeclarationDifficulty
    },{
      path:'provisionsdeclaration',
      name:'Provisionsdeclaration',
      component: Provisionsdeclaration
    },{
      path:'fullinformation',
      name:'Fullinformation',
      component: Fullinformation
    },{
      path:'hometeamapp',
      name:'Hometeamapp',
      component: Hometeamapp
    }]
    }
  ]
})


router.beforeEach((to, from, next) => {
  // 检查用户是否已登录，以及他们的令牌类型
  const userToken = localStorage.getItem('userToken');
  const adminToken = localStorage.getItem('adminToken');

  if (to.name === 'Admin' && (!adminToken || adminToken === 'null')) {
    // 如果用户试图访问Admin页面，但没有adminToken，则重定向到登录页面
    next('/login');
  } else if ((to.name === 'Home' || to.name === 'Admin') && (!userToken && !adminToken)) {
    // 如果用户试图访问Home或Admin页面，但没有userToken或adminToken，则重定向到登录页面
    next('/login');
  } else {
    // 如果用户有userToken，但没有adminToken，则允许访问Home页面
    // 如果用户有adminToken，则允许访问Admin页面
    next();
  }
});


export default router
